<?php


namespace app\mogo\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

class Comments extends Admin{
    public function index(){
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();

        $user_table = config('user_table');
        $user_model = config('user_model');
        $user_field = config('user_filed');
        $data = Db::view('dp_mogo_comments',true) //TODO 需要修改
             ->view($user_table,"{$user_field['nickname']},{$user_field['avatar']}","{$user_table}.id=dp_mogo_comments.user_id")
//            ->where('type','skill')
            ->where($map)
            ->order($order)
//            ->order('id desc')
            ->paginate()->each(function($item){
                // 对分页数据可以做额外处理
                return $item;
            });

//        halt($data);
        return ZBuilder::make('table')
            ->setTableName('mogo_comments')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['avatar','昵称','picture'],
                ['nickname','昵称'],
                ['content','内容'],
                ['visible','是否可见','switch'],
                ['create_time','创建时间','datetime'],
            ])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            ->addFilter(['title' => 'admin_action', 'username'])
            ->setSearch(['id' => 'ID', 'title' => '标题'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Skill');
            if($result!==true) $this->error($result);
            // 第三部 存储
            Post::createSkill($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'title', '标题'],
                ['image', 'pic', '配图'],
                ['textarea','content','内容'],
                ['select','cate_id','分类','',\app\xiaoan\model\Cate::where('type','skill')->column('id,name')],
            ])
            ->fetch();
    }

}